<?php
include "core/funcs/file.php";
include "core/funcs/time.php";
include "core/funcs/encode.php";

date_default_timezone_set('Asia/Shanghai');
echo "更新版本号","\r\n";
$version = '3.3.' . now_date_str('');
file_put_contents("./core/version.php", "<?php define('version', '{$version}');");
$time = now_str('', '', '');

echo "清理旧打包文件","\r\n";
@unlink("core/funcs/allinone.phpmerge");
@unlink("core/funcs/allinone.php");
copy("core/funcs/QAppDB.php", "public/install/QAppDB.php");
rmdir_recusive("../dist", 1);
echo "合并函数","\r\n";
$allinone = "";
if (file_exists("core/funcs/") && $file_list = scandir("core/funcs/")) {
    foreach ($file_list as $item) {
        if ($item != "." && $item != "..") {
            $allinone .= file_get_contents("core/funcs/{$item}");
        }
    }
}

$allinone = str_replace("<?php", '', $allinone);
file_put_contents("core/funcs/allinone.phpmerge", "<?php \r\n" . $allinone);

echo "复制最新src文件","\r\n";
file_copy_recusive("../src/", "../dist/");
file_copy("../README.md", "../dist/README.md");

echo "清理dist中的logs、cache等无用文件","\r\n";
rmdir_recusive("../dist/logs/",1);
rmdir_recusive("../dist/.idea");
rmdir_recusive("../dist/public/cache",1);
rmdir_recusive("../dist/cache");
rmdir_recusive("../dist/application/cache");

//打包Full版本
@unlink("../QAPP-DIST-Full-{$version}-{$time}.zip");
echo "生成Full版本","\r\n";
echo zip("../dist", "../QAPP-DIST-Full-{$version}-{$time}.zip") ? "成功" : "失败";

//清理生成Standard版本
echo "清理生成Standard版本","\r\n";
echo "清理funcs目录","\r\n";
if (file_exists("../dist/core/funcs") && $file_list = scandir("../dist/core/funcs")) {
    foreach ($file_list as $item) {
        if ($item != "." && $item != "..") {
            if (file_ext($item) === "php") {
                @unlink("../dist/core/funcs/" . $item);
            }
        }
    }
}
echo "清理application目录中的data、view、model","\r\n";
@unlink(" ../dist/core/func.php");
@unlink("../dist/application/apphash.php");
rmdir_recusive("../dist/application/controller/",1);
rmdir_recusive("../dist/application/data/");
rmdir_recusive("../dist/application/libs/blog");
rmdir_recusive("../dist/application/view/blog");
rmdir_recusive("../dist/application/view/blade");
rmdir_recusive("../dist/application/view/docs");
rmdir_recusive("../dist/application/view/mustache");
rmdir_recusive("../dist/application/view/plates");
rmdir_recusive("../dist/application/view/twig");
rmdir_recusive("../dist/application/model");
rmdir_recusive("../dist/application/languages");
rmdir_recusive("../dist/public/uploadfile");
rmdir_recusive("../dist/public/assets/blog");
rmdir_recusive("../dist/public/assets/docs");
rmdir_recusive("../dist/public/assets/img",1);

echo "重建dist中的cache、data、controller等必要目录","\r\n";
@mkdir("../dist/application/cache",0777);
@mkdir("../dist/application/data",0777);
@mkdir("../dist/application/libs",0777);
@mkdir("../dist/application/controller",0777);
file_copy("application/controller/index.php", "../dist/application/controller/index.php");

@mkdir("../dist/application/languages",0777);
@mkdir("../dist/logs",0777);
@mkdir("../dist/public/uploadfile",0777);

//rm -rf ../dist/public/config.php
@unlink("../dist/public/config.php");
@unlink("../dist/public/php_errors.log");
@unlink("../dist/public/admin/php_errors.log");

//rm -rf ../dist/dist.sh
@unlink("../dist/dist.sh");
@unlink("../dist/dist.php");
@unlink("../dist/test.php");

echo "清理测试文件和配置","\r\n";
rmdir_recusive("../dist/tests");
@unlink("../dist/phpunit.xml");
@unlink("../dist/phpdoc.phar");
@unlink("../dist/phpunit-9.0.1.phar");

//echo "<?php " > ../dist/application/libs/adminhooks.php
file_put_contents("../dist/application/libs/autoload.php", <<<TXT
<?php
include __DIR__."/lib.php";
include __DIR__."/adminhooks.php";
include __DIR__."/webhooks.php";
include __DIR__."/libview.php";
//在此处添加你自己的类库
//include __DIR__."/your/libs.php";
TXT);

file_put_contents("../dist/application/libs/adminhooks.php", "<?php ");

file_put_contents("../dist/application/router.php", <<<TXT
<?php
!defined('QAPP') AND exit('Forbidden');
/**
 * 自定义路由，默认路由
 * 执行顺序：
 * 1、router.php
 * 2、后台路由配置
 * 3、栏目页面路由定义
 *
 * method支持 get post put delete,  options将进行自动管理
 * access，可配置单个路由的跨域设置，可填写允许跨域的域名数组，不带http和https，如['qapp.1e9.cc','baidu.com']，也可使用"*"表示任意域名访问
 */
router::addRouter('','all', '/;/index.html', 'index');
TXT);
echo "生成合并allinone.php","\r\n";
file_put_contents("../dist/application/libs/webhooks.php", "<?php ");
@rename("../dist/core/funcs/allinone.phpmerge", "../dist/core/funcs/allinone.php");
@rename("../dist/core/func-dist.php", "../dist/core/func.php");

//打包Standard版本
@unlink("../QAPP-DIST-Standard-{$version}-{$time}.zip");
echo "生成Standard版本","\r\n";
echo zip("../dist", "../QAPP-DIST-Standard-{$version}-{$time}.zip") ? "成功" : "失败";
